<?php


class latest_result
{

	var $eventDate;
	var $description;
	
	function latest_result($data)
	{
		global $lang;
				
		$desc = sprintf($lang['RaceResult' . rand(0,3)], $data['pf_drivername'], $data['d_title'], $data['t_fullTitle']);
	
		$this->eventDate = $data['ed_dateF'];
		$this->description = '<a href="events.php?mode=results&id=' . $data['r_event'] . '&race=' .
							 $data['d_id'] . '-' . $data['r_race'] . '">' . $desc . '</a>';
		
	}
	
	function render()
	{
		$eventDate = $this->eventDate;
		$description = $this->description;
		
		global $skin_dir;
		
		include $skin_dir . 'tpl/resultsBrief.tpl';
	}

}

class latest_results
{
	var $renderList = Array();
	var $message;

	function latest_results()
	{
		global $db, $lang;
	
		$q = "select pf_drivername, date_format(ed_date, '%b %e, %Y') as ed_dateF, r_event, r_race, d_id, d_title, t_fullTitle
			  from lc_eventdates, lc_events, bb_profile_fields_data, lc_raceresults, lc_eventsignups, lc_divisions, lc_tracks
			  where e_id = ed_event AND ed_division = es_division AND r_event = e_id AND es_event = e_id AND
			        es_driver = user_id AND es_driver = r_driver AND ed_division = d_id AND t_id = e_track AND
			        r_raceposition = 1
			  order by ed_date DESC, d_order ASC, r_race DESC
			  limit 0,10";
			  
		
		// Run query
		$res = $db->sql_query($q);
		while ( $row = $db->sql_fetchrow($res)  )
		{
			$this->renderList[] = new latest_result($row);
		}
		
		include_once('includes/addMessage.php');
		$this->message = new addMessage('',$lang['LatestResults']);
	}
	
	function render()
	{
		global $skin_dir;
		
		$this->message->render();
				
		include $skin_dir . 'tpl/tableStart.tpl';
		
		foreach($this->renderList as $result)
		{
			$result->render();
		}
		
		include $skin_dir . 'tpl/tableEnd.tpl';
	}
}


class random_result
{
	var $data = Array();
	var $valid = false;
	var $message;

	function random_result()
	{
		global $db, $lang;
	
		$x = rand(0,5);
		$q = "select pf_drivername, user_id, date_format(ed_date, '%b %e, %Y') as ed_dateF, r_event, r_race, d_id, d_title, t_fullTitle, 
		             cs_image, cs_imageThmb, c_id, c_title, c_smallTitle, c_logoThmbURL, c_logoURL, d_logo, d_logoThmb, t_logo, t_logoThmb
			  from lc_eventdates, lc_events, bb_profile_fields_data, lc_raceresults, lc_eventsignups, lc_divisions, lc_tracks, lc_champsignups, lc_championships
			  where e_id = ed_event AND ed_division = es_division AND r_event = e_id AND es_event = e_id AND 
			        cs_driver = es_driver AND cs_champ = e_championship AND e_championship = c_id AND
			        es_driver = user_id AND es_driver = r_driver AND ed_division = d_id AND t_id = e_track AND
			        r_raceposition = 1
			  order by ed_date DESC, d_order ASC, r_race DESC
			  limit " . $x . ",1";
			  
		// Run query
		$res = $db->sql_query($q);
		if ( $this->data = $db->sql_fetchrow($res)  )
		{
			$this->valid = true;
			$this->data = formatArrayOutput($this->data);
		}
	}
	
	function render()
	{
		global $skin_dir, $lang;
		
		if($this->valid)
		{
			$title = $lang['NewsRaceWinner'];
			
			$skipImageStart = '<!--';
			$skipImageEnd = '-->';
			$image = '';
                        $division = $this->data['d_title'] == $this->data['c_title'] ? $this->data['d_title'] : $this->data['c_title'] . ' ' . $this->data['d_title'];
			$imageMessage = $this->data['ed_dateF'] . ': ' . sprintf($lang['RaceResult' . rand(0,3)],
                                $this->data['pf_drivername'],
                                $division,
                                $this->data['t_fullTitle']);
			
			if($this->data['cs_image'] != '' && $this->data['cs_imageThmb'] != '')
			{
				$skipImageStart = '';
				$skipImageEnd = '';
				$image = '<a href="' . $this->data['cs_image'] . '" rel="lightbox[randomimages]" title="' . 
							$imageMessage . '"><img src="' . $this->data['cs_imageThmb'] . '"/></a>';
			}
			else if($this->data['t_logo'] != '' && $this->data['t_logoThmb'] != '')
			{
				$skipImageStart = '';
				$skipImageEnd = '';
				$image = '<a href="' . $this->data['t_logo'] . '" rel="lightbox[randomimages]" title="' . 
							$imageMessage . '"><img src="' . $this->data['t_logoThmb'] . '"/></a>';
			}
			else if($this->data['d_logo'] != '' && $this->data['d_logoThmb'] != '')
			{
				$skipImageStart = '';
				$skipImageEnd = '';
				$image = '<a href="' . $this->data['d_logo'] . '" rel="lightbox[randomimages]" title="' . 
							$imageMessage . '"><img src="' . $this->data['d_logoThmb'] . '"/></a>';
			}
			else if($this->data['c_logoURL'] != '' && $this->data['c_logoThmbURL'] != '')
			{
				$skipImageStart = '';
				$skipImageEnd = '';
				$image = '<a href="' . $this->data['c_logoURL'] . '" rel="lightbox[randomimages]" title="' . 
							$imageMessage . '"><img src="' . $this->data['c_logoThmbURL'] . '"/></a>';
			}
			
			$driver = '<a href="drivers.php?id=' . $this->data['user_id'] . '">' . $this->data['pf_drivername'] . '</a>';
			$result = '<a href="events.php?mode=results&id=' . $this->data['r_event'] . '&race=' .
							 $this->data['d_id'] . '-' . $this->data['r_race'] . '">' . $lang['NewsViewResult'] . '</a>';
			
			include $skin_dir . 'tpl/randomResult.tpl';
			
		}
	}
}
class random_champion
{
	var $data = Array();
	var $valid = false;
	var $message;

	function random_champion()
	{
		global $db, $lang;
	
		$x = rand(0,3);
		$q = "select pf_drivername, date_format(c_lastRace, '%b %e, %Y') as c_date, hc_title, c_id, user_id, cs_image, cs_imageThmb, 
					 c_logoThmbURL, c_logoURL, d_logo, d_logoThmb, d_title, c_title
			  from lc_championships, bb_profile_fields_data, lc_honours_cat, lc_honours_ent, lc_divisions, lc_tracks, lc_champsignups
			  where he_rank = 1 AND he_cat = hc_id AND hc_division = d_id  AND d_champid = c_id AND hc_order AND hc_cat = 0 
			  		AND he_item = user_id AND cs_driver = user_id AND cs_champ = c_id
			  group by d_id
			  order by c_lastRace DESC, d_order ASC
			  limit " . $x . ",1";

		// Run query
		$res = $db->sql_query($q);
		if ( $this->data = $db->sql_fetchrow($res)  )
		{
			$this->valid = true;
			$this->data = formatArrayOutput($this->data);
		}
	}
	
	function render()
	{
		global $skin_dir, $lang;
		
		if($this->valid)
		{
			$title = $lang['NewsChampion'];
			
			$skipImageStart = '<!--';
			$skipImageEnd = '-->';
			$image = '';
                        $division = $this->data['d_title'] == $this->data['c_title'] ? $this->data['d_title'] : $this->data['c_title'] . ' ' . $this->data['d_title'];
			$imageMessage = $this->data['c_date'] . ': ' . sprintf($lang['ChampResult' . rand(0,3)], $this->data['pf_drivername'], $division . ' ' . $this->data['hc_title']);
			
			if($this->data['cs_image'] != '' && $this->data['cs_imageThmb'] != '')
			{
				$skipImageStart = '';
				$skipImageEnd = '';
				$image = '<a href="' . $this->data['cs_image'] . '" rel="lightbox[randomimages]" title="' . 
							$imageMessage . '"><img src="' . $this->data['cs_imageThmb'] . '"/></a>';
			}
			else if($this->data['d_logo'] != '' && $this->data['d_logoThmb'] != '')
			{
				$skipImageStart = '';
				$skipImageEnd = '';
				$image = '<a href="' . $this->data['d_logo'] . '" rel="lightbox[randomimages]" title="' . 
							$imageMessage . '"><img src="' . $this->data['d_logoThmb'] . '"/></a>';
			}
			else if($this->data['c_logoURL'] != '' && $this->data['c_logoThmbURL'] != '')
			{
				$skipImageStart = '';
				$skipImageEnd = '';
				$image = '<a href="' . $this->data['c_logoURL'] . '" rel="lightbox[randomimages]" title="' . 
							$imageMessage . '"><img src="' . $this->data['c_logoThmbURL'] . '"/></a>';
			}
			
			$driver = '<a href="drivers.php?id=' . $this->data['user_id'] . '">' . $this->data['pf_drivername'] . '</a>';
			$result = '<a href="championships.php?mode=honours&id=' . $this->data['c_id'] . '">' . $lang['NewsViewHonours'] . '</a>';
			
			include $skin_dir . 'tpl/randomResult.tpl';
			
		}
	}
}

class random_event
{
	var $data = Array();
	var $valid = false;
	var $message;

	function random_event()
	{
		global $db, $lang;
	
		$q = "SELECT  DISTINCT e_id, t_logo, t_logoThmb, t_fullTitle, c_title, c_smallTitle, c_id, c_logoThmbURL, c_logoURL, e_title, e_openSignUps, 
						t_location, e_description, l_imageThmb, DATE_FORMAT(ed_date, '%D %b, %l:%i%p') as ed_dateF,
					 (SELECT e.ed_date FROM lc_eventdates e WHERE e.ed_event = e_id ORDER BY e.ed_date ASC LIMIT 0,1) as ed_date2 
					  FROM " . EVENTDATES_TABLE . ", " . EVENTS_TABLE .", " . CHAMPIONSHIPS_TABLE .  ", " . TRACKS_TABLE . ", " . LICENSES_TABLE . "
					  WHERE e_track = t_id AND ed_event = e_id AND e_championship = c_id AND c_enabled = 1 AND c_license = l_id AND 
					        ed_date > NOW() AND ed_date < date_add(NOW(), INTERVAL 2 WEEK) AND e_openSignUps = 1 
					  ORDER BY RAND() LIMIT 1";
		// Run query
		$res = $db->sql_query($q);
		if ( $this->data = $db->sql_fetchrow($res)  )
		{
			$this->valid = true;
			$this->data = formatArrayOutput($this->data);
		}
	}
	
	function render()
	{
		global $skin_dir, $lang;
		
		if($this->valid)
		{
			$title = $lang['NewsUpcomingEvent'];
			
			$skipImageStart = '<!--';
			$skipImageEnd = '-->';
			$image = '';
			$imageMessage = sprintf($lang['EventSignUpDetails'], $this->data['c_title'] . ' - ' . $this->data['e_title'], $this->data['t_fullTitle'], $this->data['ed_dateF']);
			
			if($this->data['t_logo'] != '' && $this->data['t_logoThmb'] != '')
			{
				$skipImageStart = '';
				$skipImageEnd = '';
				$image = '<a href="' . $this->data['t_logo'] . '" rel="lightbox[randomimages]" title="' . 
							$imageMessage . '"><img src="' . $this->data['t_logoThmb'] . '"/></a>';
			}
			else if($this->data['c_logoURL'] != '' && $this->data['c_logoThmbURL'] != '')
			{
				$skipImageStart = '';
				$skipImageEnd = '';
				$image = '<a href="' . $this->data['c_logoURL'] . '" rel="lightbox[randomimages]" title="' . 
							$imageMessage . '"><img src="' . $this->data['c_logoThmbURL'] . '"/></a>';
			}
			
			$driver = $this->data['c_smallTitle'] . ' - ' . $this->data['e_title'] . '<br>' . $this->data['t_fullTitle'];
			$result = '<a href="events.php?mode=entrants&id=' . $this->data['e_id'] . '">' . $lang['NewsEventSignup'] . '</a>';
			
			include $skin_dir . 'tpl/randomResult.tpl';
			
		}
	}
}

class random_championship
{
	var $data = Array();
	var $valid = false;
	var $message;

	function random_championship()
	{
		global $db, $lang;
	
		$q = "SELECT *
			  FROM ". CHAMPIONSHIPS_TABLE .", " .  LICENSES_TABLE . " 
			  WHERE c_license = l_id AND c_enabled = 1 AND c_openSignUps = 1 AND c_lastRace > NOW()
			  ORDER BY RAND() LIMIT 1";
			  
		// Run query
		$res = $db->sql_query($q);
		if ( $this->data = $db->sql_fetchrow($res)  )
		{
			$this->valid = true;
			$this->data = formatArrayOutput($this->data);
		}
	}
	
	function render()
	{
		global $skin_dir, $lang;
		
		if($this->valid)
		{
			$title = $lang['NewsOpenChampionship'];
			
			$skipImageStart = '<!--';
			$skipImageEnd = '-->';
			$image = '';
			$imageMessage = sprintf($lang['ChampionshipSignUpDetails'], $this->data['c_title'], $this->data['l_name']);
			
			if($this->data['c_logoURL'] != '' && $this->data['c_logoThmbURL'] != '')
			{
				$skipImageStart = '';
				$skipImageEnd = '';
				$image = '<a href="' . $this->data['c_logoURL'] . '" rel="lightbox[randomimages]" title="' . 
							$imageMessage . '"><img src="' . $this->data['c_logoThmbURL'] . '"/></a>';
			}
			else if($this->data['l_image'] != '' && $this->data['l_imageThmb'] != '')
			{
				$skipImageStart = '';
				$skipImageEnd = '';
				$image = '<a href="' . $this->data['l_image'] . '" rel="lightbox[randomimages]" title="' . 
							$imageMessage . '"><img src="' . $this->data['l_imageThmb'] . '"/></a>';
			}
			
			$driver = $this->data['c_title'] . '<br>' . $this->data['l_name'];
			$result = '<a href="championships.php?mode=entrants&id=' . $this->data['c_id'] . '">' . $lang['NewsEventSignup'] . '</a>';
			
			include $skin_dir . 'tpl/randomResult.tpl';
			
		}
	}
}
class latest_champion
{

	var $eventDate;
	var $description;
	
	function latest_champion($data)
	{
		global $lang;
				
		$desc = sprintf($lang['ChampResult' . rand(0,3)], $data['pf_drivername'], $data['hc_title']);
	
		$this->eventDate = $data['c_date'];
		$this->description = '<a href="championships.php?mode=honours&id=' . $data['c_id'] . '">' . $desc . '</a>';
		
	}
	
	function render()
	{
		$eventDate = $this->eventDate;
		$description = $this->description;
		
		global $skin_dir;
		
		include $skin_dir . 'tpl/resultsBrief.tpl';
	}

}

class latest_champions
{
	var $renderList = Array();
	var $message;

	function latest_champions()
	{
		global $db, $lang;
	
		$q = "select pf_drivername, date_format(c_lastRace, '%b %e, %Y') as c_date, hc_title, c_id
			  from lc_championships, bb_profile_fields_data, lc_honours_cat, lc_honours_ent, lc_divisions, lc_tracks
			  where he_rank = 1 AND he_cat = hc_id AND hc_division = d_id  AND d_champid = c_id AND hc_order AND hc_cat = 0 AND he_item = user_id
			  group by d_id
			  order by c_lastRace DESC, d_order ASC
			  limit 0,5";
			  
		
		// Run query
		$res = $db->sql_query($q);
		while ( $row = $db->sql_fetchrow($res)  )
		{
			$this->renderList[] = new latest_champion($row);
		}
		
		include_once('includes/addMessage.php');
		$this->message = new addMessage('',$lang['LatestChampions']);
	}
	
	function render()
	{
		global $skin_dir;
		
		$this->message->render();
				
		include $skin_dir . 'tpl/tableStart.tpl';
		
		foreach($this->renderList as $result)
		{
			$result->render();
		}
		
		include $skin_dir . 'tpl/tableEnd.tpl';
	}
}

?>